

// ----------------- 修改 -----------------
const curProjectPath = '/sky-take-out';

// ---------------------------------------

// 创建app实例
const express = require('express');
const app = express();

// 设置存储引擎
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        const uploadDir = './public/' + curProjectPath;
        // 确保目录存在
        if (!fs.existsSync(uploadDir)) {
            fs.mkdirSync(uploadDir, { recursive: true })
        }
        cb(null, uploadDir); // 上传文件的目录
    },
    filename: (req, file, cb) => {
        // 为文件生成唯一的名称
        cb(null, crypto.randomUUID() + path.extname(file.originalname));
    }
});
// 创建 multer 实例
const upload = multer({ storage: storage });

// 使用静态文件目录
app.use('/image', express.static(path.join(__dirname, './public/' + curProjectPath)));

// 上传接口
app.post('/uploadImage', upload.single('image'),(req, res) => {
    // 获取文件路径
    const path = req.file.path;
    const thisPath = 'http://localhost:3090/image/' + path.split('\\')[2];

    res.status(200).json({
        code: 1,
        msg: "success",
        data: thisPath
    })
})

// 监听端口
app.listen(3090, () => {
    console.log('App listening at http://localhost:3090');
})